package top.yaofengqiao.springcloudsimple.common.util;

import lombok.Data;
import top.yaofengqiao.springcloudsimple.common.constant.ResponseCode;

/**
 * @author yfq
 * @date 2024/3/18 13:49
 * @description 统一返回结果
 */
@Data
public class R<T> {
    private Integer code;

    private String msg;

    private T data;

    public static <T> R<T> ok() {
        return R.ok(null);
    }

    public static <T> R<T> ok(T data) {
        return buildResponse(ResponseCode.SUCCESS.getCode(), ResponseCode.SUCCESS.getInfo(), data);
    }

    public static <T> R<T> fail(ResponseCode responseCode) {
        return fail(responseCode.getCode(), responseCode.getInfo());
    }

    public static <T> R<T> fail(Integer code, String msg) {
        return buildResponse(code, msg, null);
    }

    public static <T> R<T> buildResponse(Integer code, String msg, T data) {
        R<T> res = new R<>();
        res.setCode(code);
        res.setMsg(msg);
        res.setData(data);

        return res;
    }

}
